package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.facade.request.BankCardPasswdVerifyRequest;
import cc.rengu.igas.smps.facade.response.BankCardPasswdVerifyResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * 本行卡密码校验服务
 * Created by wangcheng on 2021/4/8 9:42
 */
public class BankCardPasswordVerifyService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        BankCardPasswdVerifyRequest bankCardPasswdVerifyRequest = new BankCardPasswdVerifyRequest();
        BankCardPasswdVerifyResponse bankCardPasswdVerifyResponse = new BankCardPasswdVerifyResponse();
        try {
            ConvertUtil.convertOutput(bankCardPasswdVerifyRequest);
            bankCardPasswdVerifyResponse.setHeader(bankCardPasswdVerifyRequest.getHeader());
            String acctNo = bankCardPasswdVerifyRequest.getAcctNo();
            String password = bankCardPasswdVerifyRequest.getPassword();
            if (StringUtil.isNullorEmpty(acctNo) || StringUtil.isNullorEmpty(password)) {
                rglog.error("缺少一个或多个业务参数！");
                bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                /* 演示环境默认返回成功 */
                if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
                    bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                    bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                } else {
                    int iReturnCode = schedule.callService(SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, SmpsParamConstant.DEFAULT_SERVICE_LABEL);
                    if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                        bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                        bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        //TODO 根据具体项目通道返回结果赋值
//                        bankCardPasswdVerifyResponse.setCertType("");
//                        bankCardPasswdVerifyResponse.setCertNo("");
//                        bankCardPasswdVerifyResponse.setMobileNo("");
//                        bankCardPasswdVerifyResponse.setAcctNo("");
                    } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, iReturnCode);
                        bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    } else {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.BANK_CORE_COMMUNICATION_CLIENT, iReturnCode);
                        bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                        bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                    }
                }
            }
            ConvertUtil.convertInput(bankCardPasswdVerifyResponse);
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            bankCardPasswdVerifyResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bankCardPasswdVerifyResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
